<!--
必须在子组件设置相同的组id【groupId】才能使组生效
-->
<template>
	<view>
		<slot />
	</view>
</template>

<script>
	export default {
		name:"viCollapseGroup",
		props:{
			//组的初始化在子组件内，这里只接收功能调用
			accordion:{
				type:[Boolean,null],
				default:true
			},
			groupId:{
				required:true,
				type:[String,null],
				default:""
			}
		},
		data() {
			return {
				currContentId:null,//触发事件的当前子组件id，暂存
			}
		},
		computed:{
			setting(){
				return {
					accordion: this.$api.isBoolean(this.accordion)? this.accordion :true,
				}
			}
		},
		methods: {
			//子组件点击事件，选项为手风琴时收起其他子组件
			onChildClick(contentId, isopen){
				if(this.setting.accordion){
					//查找子节点，对非contentId的节点执行关闭
					this.currContentId = contentId;
					this.resetChildren();
					this.$emit("change", {
						id: this.currContentId,
						open: isopen
					})
				}
			},
			resetChildren(){
				let children = this.getChildren();
				for(let item of children){
					if(item.contentId != this.currContentId){//非当前子组件执行关闭
						item.close()
					}
				}
			},
			//关闭全部
			closeAll(){
				let children = this.getChildren();
				for(let item of children){
					item.close()
				}
			},
			//打开全部
			openAll(){
				if(this.setting.accordion){
					return
				}
				let children = this.getChildren();
				for(let item of children){
					item.open()
				}
			},
			//获取子组件
			getChildren(){
				if(this.groupId){
					return this.$api.getChildren(this, "viCollapse", "groupId")
				}
				console.error("collapse-group未设置groupId");
				return []
			},
		}
	}
</script>

<style lang="scss">

</style>
